//
//  NowPlayingView.swift
//  FMRadio
//
//  Created by 范东 on 2022/7/31.
//

import UIKit

class NowPlayingView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(dynamicProvider: { traitCollection in
            if traitCollection.userInterfaceStyle == .dark{
                return UIColor(hex: "323232")
            }
            return UIColor(hex: "fafafa")
        })
        addSubview(indicatorView)
        indicatorView.snp.makeConstraints { make in
            make.height.equalTo(20)
            make.top.equalTo(14.5)
            make.centerX.equalToSuperview()
        }
        indicatorView.addSubview(indicator)
        indicatorView.addSubview(indicatorLabel)
        indicator.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(5)
            make.centerY.equalToSuperview()
        }
        indicatorLabel.snp.makeConstraints { make in
            make.left.equalTo(self.indicator.snp.right).offset(5)
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalToSuperview()
        }
        
        indicatorLabel.text = NSLocalizedString("No Radio", comment: "")
        indicator.state = .paused
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var _model : RadioResponseModelRadio!
    public var model : RadioResponseModelRadio!{
        set{
            _model = newValue
            indicatorLabel.text = model.name
        }
        get{
            return _model
        }
    }
    
    var _playState : NAKPlaybackIndicatorViewState = .stopped
    public var playState : NAKPlaybackIndicatorViewState{
        set{
            _playState = newValue
            indicator.state = playState
        }
        get{
            return _playState
        }
    }
    
    
    lazy var indicator: NAKPlaybackIndicatorView = {
        let indicator = NAKPlaybackIndicatorView(style: .iOS10())
        indicator?.sizeToFit()
        return indicator!
    }()
    
    lazy var indicatorLabel: UILabel = {
        let indicatorLabel = UILabel(frame: .zero)
        indicatorLabel.textColor = UIColor(dynamicProvider: { traitCollection in
            if traitCollection.userInterfaceStyle == .dark{
                return UIColor(hex: "ffffff")
            }
            return UIColor(hex: "333333")
        })
        indicatorLabel.font = .systemFont(ofSize: 15)
        indicatorLabel.sizeToFit()
        return indicatorLabel
    }()
    
    lazy var indicatorView: UIView = {
        let indicatorView = UIView(frame: .zero)
        return indicatorView
    }()

}
